<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/8
 * Time: 10:51
 */

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Image;
class Zixun extends Controller{

//    资讯管里
    public function zixun(){
        $stu=db('zixun')
          ->alias('z')
          ->field('z.createtime,z.title,z.picture,z.content,z.id,z.flg,z.zid')
          ->order('z.zid desc')
          ->paginate(7);
        $page=$stu->render();
        $res=db('zixun')
            ->count();
        return view('zixun',['res'=>$stu,'sum'=>$res,'page'=>$page]);
    }
    //查看资讯内容
    public function chakan(){
        $zid=input('zid');
        //echo $zid;die;
        $zixun=db('zixun')
            ->alias('z')
            ->where('zid',$zid)
            ->field('z.createtime,z.title,z.picture,z.content,z.id,z.flg,z.zid')
            ->find();
        $arr7=array_filter(explode(',',$zixun['id']));
        if(empty($arr7))
        {
            $zixun['uname']='这篇文章还没有人阅读！';
        }
        else
        {
            foreach ($arr7 as $value)
            {
                $name=db('user')->where('uid',$value)->field('uname,nickname')->find();
                if($name['uname']=='')
                {
                    $zi[]=$name['nickname'];
                }
                else
                {
                    $zi[]=$name['uname'];
                }
                //echo $name['uname'];
            }
            $zixun['uname']=implode(',',$zi);
        }

        return view('chakan',['zixun'=>$zixun]);
    }
    //文件上传代码--带缩率图
    public function uploads($file) {
            $fileName = md5(time());
            $fileRoute = ROOT_PATH . '/upload/news/';
            // 创建文件夹
            if (!file_exists($fileRoute)) {
                mkdir($fileRoute);
            }
            $info = $file->move($fileRoute, $fileName);
            if ($info) {
                // 缩略图
                $photo = '/upload/news/'.$info->getFilename();

            } else {
                // 上传失败获取错误信息
                return $file->getError();
            }
        return $photo;
    }

//    资讯管理添加
    public function zixunadd(){
        if(request()->isajax()){
          
            $file = request()->file('file');//return ['code'=>1,'msg'=>'添加成功',$file];
            $img= $this->uploads($file);
            $title=input('title');//资讯标题
            $time=date("Y-m-d H:i:s");//获取增加时间
            $content=input('content');//咨询内容
            $rs = db('zixun')
                ->insert([
                    'title'=>$title,
                    'content'=>$content,
                    'picture'=>$img,
                    'createtime'=>$time,
                    'flg'=>0
                ]);

            if($rs){
                    return ['code'=>1,'msg'=>'添加成功'];
                }else{
                    return ['code'=>0,'msg'=>'添加失败'];
            }
        }else{
            return view('zixunadd');
        }
    }

    public function zixundel()
    {
        $zid=input("zid");
        $rs=db('zixun')->where('zid',$zid)->find();
        $ulg=$rs['flg'];
        if ($ulg==1){
            $res=db('zixun')
                ->where('zid',$zid)
                ->update(['flg'=>0]);
        }else{
            $res=db('zixun')
                ->where('zid',$zid)
                ->update(['flg'=>1]);
        }
        //return $res;
        if(!empty($res)){
            return 1;//array('code'=>1,'msg'=>"操作成功");
        }else{
            return 0;//array('code'=>0,'msg'=>"操作失败");
        }
    }
  public function paylist(){
        $res = db('zpay')
            ->alias('p')
            ->join('f_user u','p.uid=u.uid')
            ->join('f_zixun z','z.zid = p.zid')
          	->where('p.flg','已支付')
          	->field('p.createtime,p.flg,p.uid,p.zid,p.price,u.uname,u.nickname,z.title')
          	->order('createtime desc')
            ->paginate(10);
        $page=$res->render();
        $tiao = db('zpay')
            ->where('flg','已支付')
            ->count();//dump($res);die;
        return view("paylist",['res'=>$res,'tiao'=>$tiao,'page'=>$page]);
    }
  	public function update(){
    	 if(request()->isAjax())
        {//echo 3;die;
            $zid = input('zid');
           	 $file = request()->file('file');//return ['code'=>1,'msg'=>'添加成功',$file];
           if($file!=''){             
            $img= $this->uploads($file);
           }else{
           	$img='';
           }//dump($img);die;
            $title=input('title');//资讯标题
            $time=date("Y-m-d H:i:s");//获取增加时间
            $content=input('content');//咨询内容
           //ie;
           //dump($zid);die;
           if($img!=''){
            $rs = db('zixun')
              ->where('zid',$zid)
                ->update([
                    'title'=>$title,
                    'content'=>$content,
                    'picture'=>$img,
                    'createtime'=>$time,
                    'flg'=>0
                ]);
           }else{
           		 $rs = db('zixun')
              ->where('zid',$zid)
                ->update([
                    'title'=>$title,
                    'content'=>$content,
                    'createtime'=>$time,
                    'flg'=>0
                ]);
           }//dump($rs);
            if($rs){
                    return ['code'=>1,'msg'=>'修改成功'];
                }else{
                    return ['code'=>0,'msg'=>'修改失败'];
            }
        }else{
         	$zid=input('zid');
           	$res=db('zixun')
              ->where('zid',$zid)
              ->find();
           return view("update",['res'=>$res]);
         }
    }
}
